package com.pobos.model

import com.google.appengine.api.datastore.Entity
import com.google.appengine.api.datastore.Query

class Usuario extends Model {
	
	Entity entity
	
	public Usuario(Map params){
		super(params)
	}
	
	public Usuario(Entity e){
		this.@entity = entity
	}
	
	public void addPerfil(perfil){
		this.@entity.perfil = perfil.id
	}
	
	
	static def findByNome(nome){
		
		def query = new Query(Usuario.class.simpleName)
		query.addFilter("nome", Query.FilterOperator.EQUAL, nome)
		def preparedQuery = this.datastore.prepare(query)
		
		if (preparedQuery.countEntities() == 0) {
			return null;
		} else {
			if(preparedQuery.countEntities() > 1){
				return new Usuario(preparedQuery.asList(withLimit(1))[0])
			}else{
				return new Usuario(preparedQuery.asSingleEntity())
			}
		}
	}	
	
}